
var nstr = new Date()
var ynow = nstr.getFullYear()
var mnow = nstr.getMonth()
var dnow = nstr.getDate()
var mnowReal = mnow

this.initPage()

function initPage() {
    calendar(nstr, ynow, mnow, dnow)
    monDetail(ynow, mnowReal)
    $('.year').text(ynow + '年').siblings('.month').text((mnow + 1) + '月')
}

// 显示当前年月
function monDetail(ynow, mnow) {
    mnowReal = mnow + 1
    $(".month-detail").html(ynow + '-' + mnowReal)  
}

// 判断是否为闰年
function isLeap(year) {  
    return (year%100 == 0? res =(year%400 == 0 ? 1 : 0) : res =(year%4 == 0 ? 1 : 0))
}

// 上一个月
function preMonth() {  
    
    if(mnow <= 0){
    	mnow = 11
        ynow = ynow - 1
    } else mnow--

    this.initPage()
}

// 下一个月
function nextMonth() {   

    if(mnow >= 11) {
        mnow = 0
        ynow = ynow + 1
    } else mnow++

    this.initPage()
}

// 渲染表格
function calendar(nstr, ynow, mnow, dnow) {
    
    $(".date-info tr.date").remove() 

    var nlstr = new Date(ynow, mnow, 1)
    var firstday = nlstr.getDay()
    var m_days = new Array(31, 28+isLeap(ynow),31, 30, 31, 31, 30, 31, 30, 31, 30, 31)  
    var tr_str = Math.ceil((m_days[mnow] + firstday) / 7)   
    var i, k, idx, date_str

    for(i=0; i<tr_str; i++) {
        $(".date-info tbody").append("<tr class='date'></tr>")
        for(k=0; k<7; k++) {
            idx = i * 7 + k
            date_str = idx - firstday + 1
            ;(date_str <= 0 || date_str > m_days[mnow]) ? date_str = "&nbsp;" : date_str = idx-firstday + 1
            $(".date-info .date").last().append("<td class='day'>" + date_str + "</td>")
        }
    }

    fill()
}

// 填充日历背景
// 日历中的值位当前类型对应的日期
function fill() {

    var schj = [1, 5, 13, 27]
    var cpld = [15]
    var ztmn = [22, 29]
    var yfln = [18, 20]
    var jpgm = [11, 22, 23]

    for (var i=0; i<$('.day').length; i++) {
        // 金融市场
        for (let j=0; j<schj.length; j++)
        if ($('.day')[i].innerHTML == schj[j]) $('.day')[i].style.background = '#7B6BE7'
        
        // 产品亮点
        for (let j=0; j<cpld.length; j++)
        if ($('.day')[i].innerHTML == cpld[j]) $('.day')[i].style.background = '#5A73DD'
        
        // 展厅模拟
        for (let j=0; j<ztmn.length; j++)
        if ($('.day')[i].innerHTML == ztmn[j]) $('.day')[i].style.background = '#25D0CE'
        
        // 研发理念
        for (let j=0; j<yfln.length; j++)
        if ($('.day')[i].innerHTML == yfln[j]) $('.day')[i].style.background = '#84E7AD'
        
        // 竞品观摩
        for (let j=0; j<jpgm.length; j++)
        if ($('.day')[i].innerHTML == jpgm[j]) $('.day')[i].style.background = '#FED147'


        if (Number($('.day')[i].innerHTML) == 3){
            $('.day')[i].style.border = '1px solid #fff';
            $('.day')[i].style.fontSize = 18+'px';
        }
    }   
}

// 计算最近课程安排容器的宽度
var len = $('.list ul').length
var $width = $('.list ul')[0].offsetWidth + 15 // 每个课程所占宽度
$('.list').css({ width: len * $width })

// 点击添加转训
$('.add-trans').click(function() { 
    $('.content-trans').hide().siblings('.mask-content').show()
})

// 选择课程
$('.picker-trans').click(function() {

    var picker = new mui.PopPicker()

    // 课程数据
    const list = [
        { value:'schj', text:'市场环境' },
        { value:'cpld', text:'产品亮点' },
        { value:'ztmn', text:'展厅模拟' }
    ]
    picker.setData(list)

    picker.show(function (selectItems) {
        $('.trans-val').val(selectItems[0].text)
    })
})

// 选择时间
$('.picker-date').click(function() {
    var dtPicker = new mui.DtPicker({ type: 'time' })
    dtPicker.show(function (selectItems) { 
        $('.date-val').val(selectItems.text)
    })
})

// 点击取消 隐藏弹窗
$('.cancel').click(function() { $('.mask').hide() })
    